package us.icitap.commandcenter.ui.supervisor;

import java.util.List;

import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.Property.ValueChangeListener;
import com.vaadin.data.util.BeanItemContainer;
import com.vaadin.ui.Component;
import com.vaadin.ui.Table;
import com.vaadin.ui.AbstractSelect.ItemDescriptionGenerator;
import us.icitap.commandcenter.business.ejb.persistence.Call;
import us.icitap.commandcenter.business.ejb.persistence.CallStatusEnum;
import us.icitap.commandcenter.ui.custom.CustColumnGenerator;
import us.icitap.commandcenter.ui.custom.CustLayoutWithMenu;
import us.icitap.commandcenter.ui.custom.CustTable;
import us.icitap.commandcenter.ui.shared.CallInfoWindow;
import us.icitap.commandcenter.ui.shared.CallTable;

public class ResultLayout extends CustLayoutWithMenu{

	private final CallTable table = new CallTable("Totali i telefonatave: ");
	
	
	public ResultLayout() {
		
		
		
		table.addGeneratedColumn("relatedCallNr", CustColumnGenerator.relatedCallNumberColumnGenerator);
		
		table.setVisibleColumns(new Object[]{"relatedCallNr","caller","recordDate", "callReason", "address", "description", "mainDispatcher"});
		table.setColumnHeaders(new String[]{"NR I TEL","TELEFONUESI", "DT. REGJ.", "ARSYEJA E TELEFONATES", "ADRESA", "PERSHKRIMI", "ZONA E MBULIMIT"});
		table.addGeneratedColumn("callReason", CustColumnGenerator.callReasonColumnGenerator);
		table.addGeneratedColumn("mainDispatcher", CustColumnGenerator.dispatcherColumnGenerator);
		table.setPageLength(10);
		table.setWidth("964px");
		
		table.setColumnWidth("relatedCallNr", 30);
		table.setColumnWidth("caller", 100);
		table.setColumnWidth("address", 150);
		table.setColumnWidth("description", 200);
		table.setColumnWidth("mainDispatcher", 200);
		table.setImmediate(true);
		table.setSelectable(true);
		table.setCellStyleGenerator(new Table.CellStyleGenerator() {
			
			@Override
			public String getStyle(Table source, Object itemId,
					Object propertyId) {
				if(propertyId == null){
					if(((Call)itemId).getActualStatus().equals(CallStatusEnum.N))
						return "red";
					else if(((Call)itemId).getActualStatus().equals(CallStatusEnum.WV))
						return "yellow";
					else if(((Call)itemId).getActualStatus().equals(CallStatusEnum.V))
						return "green";
				}
				return null;
			}
		});
		

		table.setSelectable(true);
		table.addValueChangeListener(new ValueChangeListener() {
			
			@Override
			public void valueChange(ValueChangeEvent event) {
				if(event.getProperty().getValue() != null){
					Call c = (Call) event.getProperty().getValue();
					CallInfoWindow ciW = new CallInfoWindow(c);
					ciW.show();
				}
				
			}
		});
		
		this.addComponent(table);
	}
	
	public Call getSelectedCall(){
		Object o = table.getValue();
		if(o != null) return (Call)o;
		return null;
	}

	public CallTable getTable() {
		return table;
	}
	
}
